﻿using Microsoft.AspNetCore.Mvc;
using MiniFox.Components;
using MiniFox.Infrastructure;
using MiniFox.Platform.Resources;
using MiniFox.Platform.Web.Filters;
using MiniFox.Utility;
using MiniFox.Web;
using MiniFox.Web.Infrastructure;

namespace MiniFox.Platform.Web
{
    /// <summary>
    /// 
    /// </summary>
    public class PinCodeController : MvcControllerBase
    {
        /// <summary>
        /// 
        /// </summary>
        [AutoWired]
        protected virtual IPinCodeService Component
        {
            get;
            set;
        }
        #region pincode validation
        /// <summary>
        /// 发送验证码
        /// </summary>
        /// <param name="signName">邮箱或手机号</param>
        /// <returns></returns>
        [HttpGet, Verify]
        public virtual Response<string> Send(string signName)
        {
            string ipAddress = Request.GetClientIP();

            var context = this.Component.CreatePincodeContext(signName, ipAddress);
            string ticket = context.Ticket;
            var response = ticket.CreateResponse();
            response.Message = ResMessage.MSG_PinCodeSent;
            return response;
        }
        /// <summary>
        /// 检查验证码
        /// </summary>
        /// <param name="ticket"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpGet]
        public virtual Response Verify(string ticket, string code)
        {
            var response = new Response();

            string ipAddress = Request.GetClientIP();

            if (!Component.MatchPinCode(ticket, code, ipAddress))
            {
                response.Code = ResErrorCode.ERR_InvalidPinCode;
                response.Message = ResMessage.ERR_InvalidPinCode;
            }

            return response;
        }
        #endregion
    }
}
